package com.googlecode.monitoring.flash.dao;


import com.googlecode.monitoring.dao.AgentConfigurationDao;
import com.googlecode.monitoring.dao.XmlUtils;
import com.googlecode.monitoring.exception.DataDaoException;
import com.googlecode.monitoring.model.AlertText;
import com.googlecode.monitoring.model.ItemToCheck;
import com.googlecode.monitoring.model.Person;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Repository;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Sergiy Doroshenko
 * Date: May 17, 2010
 * Time: 12:22:34 PM
 */
@Repository
public class XmlFlashAgentConfigurationDao extends AgentConfigurationDao {

    Logger log = Logger.getLogger(XmlFlashAgentConfigurationDao.class);

    private Resource xmlFlashConfigResource;
    private XmlUtils xmlUtils;
    private FlashAgentXmlUtils flashAgentXmlUtils;

    private List<Person> commonResponsiblePersonList;

    @Autowired
    public void setXmlUtils(XmlUtils xmlUtils) {
        this.xmlUtils = xmlUtils;
    }

    @Autowired
    public void setXmlFlashConfigResource(Resource xmlFlashConfigResource) {
        this.xmlFlashConfigResource = xmlFlashConfigResource;
    }

    @Autowired
    public void setFlashAgentXmlUtils(FlashAgentXmlUtils flashAgentXmlUtils) {
        this.flashAgentXmlUtils = flashAgentXmlUtils;
    }

    public List<Person> getCommonResponsiblePersons() throws DataDaoException {
        if (commonResponsiblePersonList == null) {
            commonResponsiblePersonList = new ArrayList<Person>();
            try {
                Document doc = xmlUtils.getDocumentFromResource(xmlFlashConfigResource, true);
                Element alertsElem = (Element) doc.getElementsByTagName("alerts").item(0);
                NodeList personNodes = alertsElem.getElementsByTagName("person");
                commonResponsiblePersonList.addAll(xmlUtils.buildPersons(personNodes));
            } catch (Throwable e) {
                log.error(e);
                throw new DataDaoException(e);
            }
        }
        return commonResponsiblePersonList;
    }

    @Override
    public AlertText getAlertText() throws DataDaoException {
        try {
            Document doc = xmlUtils.getDocumentFromResource(xmlFlashConfigResource, false);
            return xmlUtils.getAlertText(doc);
        } catch (Throwable e) {
            log.error(e);
            throw new DataDaoException(e);
        }
    }

    @Override
    public List<ItemToCheck> getAllItemsFromConfiguration() throws DataDaoException {
        try {
            Document doc = xmlUtils.getDocumentFromResource(xmlFlashConfigResource, true);
            NodeList nodes = doc.getElementsByTagName("item");
            List<ItemToCheck> items = new ArrayList<ItemToCheck>(nodes.getLength());
            log.debug("readed " + nodes.getLength() + " nodes item");
            for (int i = 0; i < nodes.getLength(); i++) {
                Node xmlItem = nodes.item(i);
                if (xmlItem.getNodeType() == Node.ELEMENT_NODE) {
                    try {
                        items.add(flashAgentXmlUtils.buildDataToCheck((Element) xmlItem));
                    } catch (Exception e) {
                        log.error(xmlItem.toString() + ";i=" + i, e);
                        throw new DataDaoException(xmlItem.toString(), e);
                    }
                }
            }
            return items;
        } catch (Throwable e) {
            throw new DataDaoException(e);
        }
    }

}
